Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose: Documentation: Document withGlobalEvents #15175

Merged
merged 1 commit into from
Apr 25, 2019

Conversation

aduth
Copy link
Member

@aduth aduth commented Apr 25, 2019

This pull request seeks to add missing documentation for the withGlobalEvents higher-order component creator.

Testing Instructions:

Verify by rendered output of "Files changed" tab that produced documentation is sensible.

There are only documentation changes. Thus, there is no expected impact on application runtime.

create or update a window event handler to invoke the callback when an event
occurs. On behalf of the consuming developer, the higher-order component
manages unbinding when the component unmounts, and binding at most a single
event handler for the entire application.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Isn't that a built in feature of addEventListener to bind once at most?

The event listener list will not contain multiple event listeners with equal type, callback, and capture, as add an event listener prevents that.

https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key words there being "equal [..] callback". Since function() {} !== function() {}, it's not reused.

Try in the console, for example:

[ ...Array( 5 ) ].forEach( ( v, i ) => document.addEventListener( 'click', () => console.log( 'click' + i ) ) )

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Isn't that a built in feature of addEventListener to bind once at most?

While it's not really relevant for change here, the question does get me wondering whether it really matters to consolidate to a single event handler. I'm trying to recall from the original implementation (#5730) whether I'd thought this was some sort of performance enhancement (i.e. one 'scroll' handler on the window rather than one for every component) and if that was the case, whether there's actually any truth to the consolidation being an optimization. I might expect a browser with its low-level language code could iterate to invoke individual callbacks of addEventListener faster than we could iterate to invoke the Listener callbacks here in JavaScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Compose /packages/compose [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants